package org.tuestudent.grouppa;

import java.util.Arrays;
import java.util.Collection;

import junit.framework.*;

import org.json.JSONException;
import org.json.JSONObject;

public class MessageFromServerTest extends TestCase {

    public void testFromJSONObject() throws JSONException {
        String[] json =
                new String[] {
                        "{" + "\"fromController\":\"\"," + "\"id\":0,"
                                + "\"responseToId\":0,"
                                + "\"serializedArguments\":\"blabla\","
                                + "\"toController\":\"\"," + "\"toTask\":\"\"}",

                        "{"
                                + "\"fromController\":\"planning\","
                                + "\"id\":42,"
                                + "\"responseToId\":12,"
                                + "\"serializedArguments\":{\"smth\":\"smthelse\"},"
                                + "\"toController\":\"chat\","
                                + "\"toTask\":\"newMessage\"}",

                        "{" + "\"fromController\": \"forum\"," + "\"id\": 01,"
                                + "\"responseToId\":032,"
                                + "\"toController\":\"racer\","
                                + "\"toTask\":\"drive\"}" };

        for (int i = 0; i < json.length; i++ ) {
            System.out
                    .println("Testing json object conversion to MessageFromServer: "
                            + json[i]);
            FromJSONObjectTest(new JSONObject(json[i]));
            System.out.println("test succeeded");
        }
    }

    public void FromJSONObjectTest(JSONObject jobject) {
        MessageFromServer message = MessageFromServer.fromJSONObject(jobject);
        assertEquals(message.fromController,
                jobject.optString("fromController"));
        assertEquals(message.id, jobject.optLong("id"));
        assertEquals(message.responseTold, jobject.optLong("responseToId"));
        assertEquals(message.toController, jobject.optString("toController"));
        assertEquals(message.toTask, jobject.optString("toTask"));
    }
}
